<?php

/**
 * job actions.
 *
 * @package    jobeet
 * @subpackage job
 * @author     Your name here
 */
class jobActions extends sfActions {

    public function executeIndex(sfWebRequest $request) {

        //$criteria = new Criteria();
        //$criteria->add(JobeetJobPeer::EXPIRES_AT, time(), Criteria::GREATER_THAN);
        $this->categories = JobeetCategoryPeer::getWithJobs();


        //$this->jobeet_jobs = JobeetJobPeer::getActiveJobs();
    }

    public function executeShow(sfWebRequest $request) {
        //$this->job = JobeetJobPeer::retrieveByPk($request->getParameter('id'));
        //debug($this->getRoute()->getObject());
        $this->job = $this->getRoute()->getObject(); // it provides better error messages than the above one.
        //$this->forward404Unless($this->job);// this also can be ommitted case the above one line provide exception's throwing in case of wrong credentials
        
        
        $this->getUser()->addJobToHistory($this->job);
        //$jobs = $this->getUser()->getAttribute('job_history', array());
        //array_unshift($jobs, $this->job->getId());
        //$this->getUser()->setAttribute('job_history', $jobs);
    }

    public function executeNew(sfWebRequest $request) {
        $job = new JobeetJob();
        $job->setType('full-time');
        $this->form = new JobeetJobForm($job);

        //$this->form = new JobeetJobForm();
    }

    public function executeCreate(sfWebRequest $request) {
        //$this->forward404Unless($request->isMethod(sfRequest::POST));

        $this->form = new JobeetJobForm();

        $this->processForm($request, $this->form);

        $this->setTemplate('new');
    }

    public function executeEdit(sfWebRequest $request) {
        //$this->forward404Unless($JobeetJob = JobeetJobPeer::retrieveByPk($request->getParameter('id')), sprintf('Object JobeetJob does not exist (%s).', $request->getParameter('id')));        
        //$this->form = new JobeetJobForm($this->getRoute()->getObject());
        $job = $this->getRoute()->getObject();
        $this->forward404If($job->getIsActivated());
        $this->form = new JobeetJobForm($job);
    }

    public function executeUpdate(sfWebRequest $request) {
        //$this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
        //$this->forward404Unless($JobeetJob = JobeetJobPeer::retrieveByPk($request->getParameter('id')), sprintf('Object JobeetJob does not exist (%s).', $request->getParameter('id')));

        $this->form = new JobeetJobForm($this->getRoute()->getObject());

        $this->processForm($request, $this->form);

        $this->setTemplate('edit');
    }

    public function executeDelete(sfWebRequest $request) {
        $request->checkCSRFProtection();

        //$this->forward404Unless($JobeetJob = JobeetJobPeer::retrieveByPk($request->getParameter('id')), sprintf('Object JobeetJob does not exist (%s).', $request->getParameter('id')));
        $job = $this->getRoute()->getObject();

        $job->delete();
        $this->redirect('job/index');
    }

    protected function processForm(sfWebRequest $request, sfForm $form) {
        $form->bind(
                $request->getParameter($form->getName()), $request->getFiles($form->getName())
        );

        if ($form->isValid()) {
            $job = $form->save();

            $this->redirect($this->generateUrl('job_show', $job));
        }
    }

    public function executePublish(sfWebRequest $request) {

        $request->checkCSRFProtection();
        $job = $this->getRoute()->getObject();
        $job->publish();
        $this->getUser()->setFlash('notice', sprintf('Your job is now online for %s days.', sfConfig::get('app_active_days')));
        $this->redirect('job_show_user', $job);
    }

    public function executeExtend(sfWebRequest $request) {
        $request->checkCSRFProtection();
        $job = $this->getRoute()->getObject();
        $this->forward404Unless($job->extend());
        $this->getUser()->setFlash('notice', sprintf('Your job validity has been extended until %s.', $job->getExpiresAt('m/d/Y')));
        $this->redirect('job_show_user', $job);
    }

}
